<template>
  <div class="layout-container">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div class="header-content">
          <div class="logo">
            <el-icon><Compass /></el-icon>
            <span>校园失物招领</span>
          </div>
          <el-menu mode="horizontal" :default-active="activeMenu" router>
            <el-menu-item index="/home">首页</el-menu-item>
            <el-menu-item index="/lost">失物查询</el-menu-item>
            <el-menu-item index="/found">招领查询</el-menu-item>
            <el-menu-item index="/publish">发布信息</el-menu-item>
            <el-menu-item index="/announcement">公告</el-menu-item>
          </el-menu>
          <div class="user-info">
            <el-dropdown v-if="userStore.isLoggedIn">
              <span class="user-name">
                {{ userStore.userInfo.username }}
                <el-icon><CaretBottom /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="$router.push('/my-publish')">我的发布</el-dropdown-item>
                  <el-dropdown-item @click="$router.push('/my-claims')">我的认领</el-dropdown-item>
                  <el-dropdown-item v-if="hasRole('ADMIN')" @click="$router.push('/admin')">
                    后台管理
                  </el-dropdown-item>
                  <el-dropdown-item v-if="hasRole('SUPER_ADMIN')" @click="$router.push('/super-admin')">
                    <span style="color: #f56c6c; font-weight: 600;">⚡ 超级管理员</span>
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-button v-else type="primary" @click="$router.push('/login')">登录</el-button>
          </div>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-main>
        <router-view />
      </el-main>

      <!-- 底部 -->
      <el-footer>
        <div class="footer-content">
          <p>© 2024 校园失物登记与招领平台. All rights reserved.</p>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)

// 检查用户是否拥有指定角色
const hasRole = (roleCode) => {
  const roles = userStore.userInfo.roles || []
  return roles.includes(roleCode)
}

const handleLogout = () => {
  ElMessageBox.confirm('确定退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logout()
    router.push('/login')
  })
}
</script>

<style scoped>
.layout-container {
  height: 100%;
}

.el-container {
  height: 100%;
  flex-direction: column;
}

.el-header {
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  padding: 0 20px;
}

.header-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.logo {
  display: flex;
  align-items: center;
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
  margin-right: 40px;
}

.logo .el-icon {
  font-size: 28px;
  margin-right: 10px;
}

.el-menu {
  flex: 1;
  border-bottom: none;
}

.user-info {
  margin-left: 20px;
}

.user-name {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
}

.el-main {
  background: #f5f5f5;
  padding: 20px;
  overflow-y: auto;
}

.el-footer {
  background: #fff;
  border-top: 1px solid #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
}

.footer-content {
  text-align: center;
  color: #909399;
}
</style>

